<!--历史数据-->
<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss"
                             valueFormat="YYYY-MM-DD HH:mm:ss"/>
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss"
                             valueFormat="YYYY-MM-DD HH:mm:ss"/>
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="liedrangId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openTempDialog(record)">温度明细</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

    <cattle-manager-cattle-liedown-range-data-temp  v-if="showTemp" v-model:visible="showTemp" :data="current" @done="reload" />
  </div>
</template>

<script>
import {message} from 'ant-design-vue';
import {DeviceLiedownRangsApi} from "@/api/property/cattlesports/DeviceLiedownRangsApi";
import CattleManagerCattleLiedownRangeDataTemp from './liedown-range-data-temp.vue'
import moment from "moment";

export default {
  name: 'CattleManagerCattleLiedownRangeData',
  components: {
    CattleManagerCattleLiedownRangeDataTemp
  },
  props: {
    data: Object
  },
  async mounted() {

  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '状态',
          dataIndex: 'lieType',
          customRender: function (row) {
            if (row.text == 'liedown') {
              return '躺卧';
            } else if (row.text == 'stand') {
              return '站立';
            }
          }
        },
        {
          title: '开始时间',
          dataIndex: 'beginTime'
        },
        {
          title: '结束时间',
          dataIndex: 'endTime'
        },
        {
          title: '运动量',
          dataIndex: 'step',
        },
        {
          title: '时长',
          dataIndex: 'beginTime',
          customRender: function (row) {
            let begin = row.record.beginTime;
            let end = row.record.endTime;
            let lastTime = row.record.lastTime;
            if (begin == null) {
              return '-';
            } else if (end == null) {
              if (lastTime != null) {
                let mii = Math.floor((moment(lastTime).diff(moment(begin))) / 1000);
                if (mii > 3600) {
                  const hoursi = Math.floor(mii / 3600); // 3600秒 = 1小时
                  const minutesi = Math.floor((mii % 3600) / 60); // 余下的秒数除以60得到分钟数
                  return hoursi + "小时" + minutesi + "分";
                } else if (mii > 60) {
                  return Math.ceil(mii / 60) + "分";
                } else {
                  return mii + "秒";
                }
              } else {
                return '-';
              }
            } else {
              let mi = Math.floor((moment(end).diff(moment(begin))) / 1000);
              if (mi > 3600) {
                const hours = Math.floor(mi / 3600); // 3600秒 = 1小时
                const minutes = Math.floor((mi % 3600) / 60); // 余下的秒数除以60得到分钟数
                return hours + "小时" + minutes + "分";
              } else if (mi > 60) {
                return Math.ceil(mi / 60) + "分";
              } else {
                return mi + "秒";
              }
            }
          }
        },
        {
          title: '近一次记录时间',
          dataIndex: 'lastTime'
        },
        {
          title: '近一次记录运动量',
          dataIndex: 'lastStep',
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {
        "cattleId": this.data.cattleId,
        "sortBy" : "desc",
        "orderBy" : "begin_time"
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current : null,
      // 是否显示编辑弹窗
      showDetail : false,
      showTemp : false,
   }
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({page: 1});
    }
    ,

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    reset() {
      this.where.cattleId = this.data.cattleId;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.where.sortBy = 'desc';
      this.where.orderBy = 'begin_time';
      this.$nextTick(() => {
        this.reload();
      });
    }
    ,

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    async remove(row) {
      const result = await DeviceLiedownRangsApi.delete({liedrangId: row.liedrangId});
      message.success(result.message);
      this.reload();
    }
    ,

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    openDetail(row) {
      this.current = row;
      this.showDetail = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({page, limit, where, orders}) {
      return DeviceLiedownRangsApi.findPage({...where, ...orders, pageNo: page, pageSize: limit});
    },
    openTempDialog (row) {
      this.current = row;
      this.showTemp = true;
    }
  }
};
</script>
